<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="java.io.File" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="com.bdqn.entity.News" %>
<%@ page import="java.util.Date" %>
<%@ page import="com.bdqn.service.NewsService" %>
<%@ page import="com.bdqn.service.Impl.NewsServiceImpl" %>
<%
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html; charset=UTF-8");
    /*
    处理文件上传
    先判断请求是否包含文件上传信息
     */
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    //创建新闻对象
    News news = new News();
    if(isMultipart){
        //创建文件上传的核心组件
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        /*
        文件上传后存到哪里?static/upload
        绑定绝对路径
         */
        String saveDirPath = request.getServletContext().getRealPath("static/upload");
        //判断上述目录是否存在，如果不存在就创建
        File saveDirFile = new File(saveDirPath);
        if(!saveDirFile.exists()){
            //不存在就创建目录
            saveDirFile.mkdirs();
        }
        //接收用户从前端传来的参数
        List<FileItem> items = upload.parseRequest(request);//解析用户的请求参数为list集合
        //通过迭代器遍历集合
        Iterator<FileItem> iterator = items.iterator();
        //遍历迭代器
        while (iterator.hasNext()){
            //获取遍历到的元素
            FileItem item = iterator.next();
            //判断item是否为文件域参数
            if(item.isFormField()){
                //获取普通表单字段的名称
                String fieldName = item.getFieldName();
                //item是表单域
                if("categoryId".equals(fieldName)){
                    //获取前端传来的普通字段数据，转换为整数后，为news对象的分类编号属性进行赋值，用utf-8字符集进行接收
                    news.setCategoryId(Integer.parseInt(item.getString("utf-8")));
                }else if ("title".equals(fieldName)) {
                    news.setTitle(item.getString("utf-8"));
                }else if ("author".equals(fieldName)){
                    news.setAuthor(item.getString("utf-8"));
                }else if ("summary".equals(fieldName)){
                    news.setSummary(item.getString("utf-8"));
                }else if ("content".equals(fieldName)){
                    news.setContent(item.getString("utf-8"));
                }
            }else {
                //文件域
                //处理文件上传   获取文件名称
                String fileName = item.getName();
                //创建文件对象，关联上传后的文件
                File uploadFile = new File(saveDirPath, fileName);
                item.write(uploadFile);//上传文件的操作
                news.setPicPath(fileName);//设置文件名称
            }
        }
    }
    news.setCreateDate(new Date());//设置新闻的创建日期
    //调用service的新闻增加方法
    NewsService newsService=new NewsServiceImpl();
    int count = newsService.addNews(news);
    if(count>0){
        response.sendRedirect("newsDetailList.jsp");
    }else{
        response.getWriter().write("新闻增加失败！");
    }



%>